﻿using System.Drawing;
using FBEntities.FBObjects;
using XDAFacebook;
using XFControls.Utils;

namespace XFControls.XFPanels.XFPanelItems
{
    public class XFItemGroupHead : XFItemBase
    {
        private int _height;
        private const int _imageHeight = 128;
        private const int _imageWidth = 128;
        private string _name;
        private FBGroup _group;

        public XFItemGroupHead()
        {
            _height = -1;
            Style = new XFItemStyle
                        {
                            TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                            Padding = 10,
                            BackgroundColor = Color.RoyalBlue,
                            TextColor = Color.White,
                            SelectedTextColor = Color.Black
                        };

            ItemType = XFItemType.Display;
        }

        public FBGroup Group
        {
            get { return _group; }
            set { _group = value; }
        }

        public override int GetHeight()
        {
            if (_height > 0)
                return _height;

            if (Group == null)
                return (_height = _imageHeight + Style.PaddingTop);

            int width = GetWidth();
            
            width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;
            _name = Group.name;
            _height = Style.TextFont.MeasureString(_name, new Rectangle(0, 0, width, 0)).Height + Style.PaddingBottom;


            if (_height < _imageHeight + Style.PaddingTop)
                _height = _imageHeight + Style.PaddingTop;

            _height += Style.PaddingBottom;
            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);
            int width = GetWidth(false);

            string url = _group.Picture.url + "&type=large";
            DrawImage(url, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _imageWidth, _imageHeight));

            Color textColor = getCurrentFontColor(false);

            using (var p = new Pen(Color.DarkGray))
                g.DrawLine(p, 0, y + _height, width, y + _height);
            using (var b = new SolidBrush(textColor))
            {
                width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;

                if (Group == null || string.IsNullOrEmpty(_name))
                    g.DrawString(Settings.LocalizationStrings.noStatus, Style.TextFont, b,
                                 x + (Style.PaddingLeft * 2) + _imageWidth, y + Style.PaddingRight);
                else
                    g.DrawString(_name,
                                 new Rectangle(x + (Style.PaddingLeft * 2) + _imageWidth, y + Style.PaddingRight, width, 0),
                                 Style, textColor);
            }
        }

        public override XFItemClickResult GetClickResult()
        {
            return XFItemClickResult.Nothing;
        }
    }
}